<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <button>获取数据</button>
  <img></img>
</body>
<script>
  const button = document.querySelector('button')
  const img = document.querySelector('img')

  function ajaxPromise() {
    let Promise = new Promise((resolve, reject) => {
      let xhr = new XMLHttpRequest()
      xhr.open('get',
        '',
        true)
      xhr.onreadystatechange = () => {
        if (xhr.readyState === 4) {
          if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) {
            resolve(xhr.responseURL)
          } else {
            reject(new Error(xhr.statusText))
          }
        }
      }
      xhr.send(null)
    })
  }

  button.addEventListener('click', () => {
    console.log('123')
    ajaxPromise().then(responseURL => {
        img.src = responseURL
      })
      .catch(statusText => {
        console.log(statusText)
      })
  })
</script>

</html>